package com.letoken.platform.pub.configuration;


import com.letoken.platform.pub.enums.ApiResponseEnum;
import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.PathProvider;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//@Configuration
//@EnableOpenApi
public class SwaggerConfiguration {

    @Value("${swagger.context-path:/}")
    private String swaggerContextPath;

    @Value("${swagger.host:localhost:8080}")
    private String swaggerHost;

    @Value("${swagger.title:接口文档}")
    private String swaggerTitle;

    @Value("${swagger.desc:接口文档}")
    private String swaggerDesc;

    @Value("${swagger.version:V1.0.0}")
    private String swaggerVersion;

//    private List<RequestParameter> globalRequestParameters() {
//        List<RequestParameter> requestParameters = new ArrayList<>();
//        Arrays.stream(BankApiResponseEnum.values()).forEach(errorEnums -> {
//            requestParameters.add(
//                    new RequestParameterBuilder().name(AuthKeyConstant.USER_ID).description("用户ID").build()
//            );
//        });
//        return requestParameters;
//    }

    private List<Response> globalResponses() {
        List<Response> responseList = new ArrayList<>();
//        responseList.add(new ResponseBuilder().code(String.valueOf(ApiResponseCode.SUCCESS.getCode())).description(ApiResponseCode.SUCCESS.getDesc()).build());
//        responseList.add(new ResponseBuilder().code(String.valueOf(ApiResponseCode.ERROR_001.getCode())).description(ApiResponseCode.ERROR_001.getDesc()).build());
//        responseList.add(new ResponseBuilder().code(String.valueOf(ApiResponseCode.ERROR_002.getCode())).description(ApiResponseCode.ERROR_002.getDesc()).build());
//        responseList.add(new ResponseBuilder().code(String.valueOf(ApiResponseCode.ERROR_003.getCode())).description(ApiResponseCode.ERROR_003.getDesc()).build());
        Arrays.stream(ApiResponseEnum.values()).forEach(errorEnums -> {
            responseList.add(
                    new ResponseBuilder().code(String.valueOf(errorEnums.getCode())).description(errorEnums.getDesc()).build()
            );
        });
        Arrays.stream(LetokenApiResponseEnum.values()).forEach(errorEnums -> {
            responseList.add(
                    new ResponseBuilder().code(String.valueOf(errorEnums.getCode())).description(errorEnums.getDesc()).build()
            );
        });

        return responseList;
    }

//    @Bean
//    public Docket letokenPlatform() {
//        return new Docket(DocumentationType.OAS_30)
//                .pathProvider(new PathProvider() {
//                    @Override
//                    public String getOperationPath(String operationPath) {
//                        return swaggerContextPath +operationPath; // 处理服务网关模式的路由问题
////                        return operationPath; // 处理服务网关模式的路由问题
//
//                    }
//
//                    @Override
//                    public String getResourceListingPath(String groupName, String apiDeclaration) {
//                        return null;
//                    }
//                })
//                .enable(true)
//                .apiInfo(apiInfo())
//                .groupName("letoken-platform")
//                .globalResponses(HttpMethod.GET, globalResponses())
//                .globalResponses(HttpMethod.POST, globalResponses())
//                .globalResponses(HttpMethod.PUT, globalResponses())
//                .globalResponses(HttpMethod.DELETE, globalResponses())
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.letoken.platform"))
//                .paths(PathSelectors.any())
//                .build()
//                .host(swaggerHost);
//    }


//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                .title(swaggerTitle)
//                .description(swaggerDesc)
//                .version(swaggerVersion)
//                .build();
//    }
}
